<!DOCTYPE html>
<html>
<head>
    <title>系统配置</title>
    <meta charset="utf-8">
    <!-- 指定以IE8的方式来渲染 -->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="/css/jsherp.css"/>
    <script type="text/javascript" src="/js/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/js/colorbox/jquery.colorbox-min.js"></script>
    <script type="text/javascript" src="/js/colorbox/colorboxSet.js"></script>
    <link href="/js/colorbox/colorbox.css" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css"/>
    <link type="text/css" rel="stylesheet" href="/css/common.css"/>
    <script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common/common.js"></script>
</head>
<body>
<!-- 数据显示table -->
<div id="tablePanel" class="easyui-panel" data-options="fit:true" style="padding:1px;top:300px;" title="系统配置" iconCls="icon-list">
    <div class="box-body form-inline">
        <div class="form-group">
            <label class="control-label">公司名称：</label>
            <div class="control-inline">
                <input type="text" id="searchCompanyName" name="searchCompanyName" value="" maxlength="100" class="easyui-textbox width-90"/>
            </div>
        </div>
        <div class="form-group">
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" id="searchBtn">查询</a>&nbsp;
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" id="searchResetBtn">重置</a>
        </div>
    </div>
    <table id="tableData" style="top:300px;border-bottom-color:#FFFFFF"></table>
</div>

<div id="systemConfigDlg" class="easyui-dialog" style="width:380px;padding:10px 20px"
     closed="true" buttons="#dlg-buttons" modal="true" collapsible="false" closable="true">
    <form id="systemConfigFM" method="post" novalidate>
        <table>
            <tr>
                <td>公司名称</td>
                <td style="padding:5px 10px">
                    <input name="companyName" id="companyName" class="easyui-textbox"
                           data-options="required:true,validType:'length[2,15]'" style="width: 220px;"/>
                </td>
            </tr>
            <tr>
                <td>联系人</td>
                <td style="padding:5px 10px">
                    <input name="companyContacts" id="companyContacts" class="easyui-textbox" style="width: 220px;"/>
                </td>
            </tr>
            <tr>
                <td>公司地址</td>
                <td style="padding:5px 10px">
                    <input name="companyAddress" id="companyAddress" class="easyui-textbox" style="width: 220px;"/>
                </td>
            </tr>
            <tr>
                <td>公司电话</td>
                <td style="padding:5px 10px">
                    <input name="companyTel" id="companyTel" class="easyui-textbox" style="width: 220px;"/>
                </td>
            </tr>
            <tr>
                <td>公司传真</td>
                <td style="padding:5px 10px">
                    <input name="companyFax" id="companyFax" class="easyui-textbox" style="width: 220px;"/>
                </td>
            </tr>
            <tr>
                <td>公司邮编</td>
                <td style="padding:5px 10px">
                    <input name="companyPostCode" id="companyPostCode" class="easyui-textbox" style="width: 220px;"/>
                </td>
            </tr>
            <tr>
                <td>仓库权限</td>
                <td style="padding:5px 10px;text-align:left;">
                    <input name="depotFlag" id="depotFlag" type="checkbox" style="width: 20px;height: 20px" title="勾选后需要到用户列表配置"/>
                    (勾选后需要到用户列表配置)
                </td>
            </tr>
            <tr>
                <td>客户权限</td>
                <td style="padding:5px 10px;text-align:left;">
                    <input name="customerFlag" id="customerFlag" type="checkbox" style="width: 20px;height: 20px" title="勾选后需要到用户列表配置"/>
                    (勾选后需要到用户列表配置)
                </td>
            </tr>
            <tr>
                <td>负库存</td>
                <td style="padding:5px 10px;text-align:left;">
                    <input name="minusStockFlag" id="minusStockFlag" type="checkbox" style="width: 20px;height: 20px" title="勾选后支持负库存，批次药品除外"/>
                    (勾选后支持负库存，批次药品除外)
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" id="saveSystemConfig" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:void(0)" id="cancelSystemConfig" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#systemConfigDlg').dialog('close')">取消</a>
</div>
<script type="text/javascript">
    //初始化界面
    $(function () {
        initTableData();
        ininPager();
    });

    //初始化表格数据
    function initTableData() {
        function parseFlag(value) {
            var flagName = "";
            if (value =="1") {
                flagName = "<span style='color:green'>启用</span>";
            }
            else {
                flagName = "<span style='color:red'>关闭</span>";
            }
            return flagName;
        }
        $('#tableData').datagrid({
            height: heightInfo,
            nowrap: false,
            rownumbers: false,
            //动画效果
            animate: false,
            //选中单行
            singleSelect: true,
            collapsible: false,
            pagination: true,
            //交替出现背景
            striped: true,
            pageSize: initPageSize,
            pageList: initPageNum,
            columns: [[
                {field: 'id', width: 35, align: "center", checkbox:true},
                {
                    title: '操作', field: 'op', align: "center", width: 80,
                    formatter: function (value, rec, index) {
                        var str = '';
                        str += '<img src="/js/easyui/themes/icons/pencil.png" style="cursor: pointer;" onclick="editSystemConfig(\'' + index + '\');"/>&nbsp;&nbsp;&nbsp;';
                        str += '<img src="/js/easyui/themes/icons/edit_remove.png" style="cursor: pointer;" onclick="deleteSystemConfig(' + rec.id + ');"/>';
                        return str;
                    }
                },
                {title: '公司名称', field: 'companyName', width: 150},
                {title: '联系人', field: 'companyContacts', width: 80, align: "center"},
                {title: '公司地址', field: 'companyAddress', width: 150, align: "center"},
                {title: '公司电话', field: 'companyTel', width: 120, align: "center"},
                {title: '公司传真', field: 'companyFax', width: 120, align: "center"},
                {title: '公司邮编', field: 'companyPostCode', width: 80, align: "center"},
                {
                    title: '仓库权限', field: 'depotFlag', width: 80, align: "center",
                    formatter: function (value) {
                        return parseFlag(value);
                    }
                },
                {
                    title: '客户权限', field: 'customerFlag', width: 80, align: "center",
                    formatter: function (value) {
                        return parseFlag(value);
                    }
                },
                {
                    title: '负库存', field: 'minusStockFlag', width: 80, align: "center",
                    formatter: function (value) {
                        return parseFlag(value);
                    }
                }
            ]],
            toolbar: [
                {
                    id: 'addSystemConfig',
                    text: '增加',
                    iconCls: 'icon-add',
                    handler: function () {
                        addSystemConfig();
                    }
                },'-',
                {
                    id: 'deleteSystemConfig',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        batDeleteSystemConfig();
                    }
                }
            ],
            onLoadError: function () {
                $.messager.alert('页面加载提示', '页面加载异常，请稍后再试！', 'error');
                return;
            }
        });
        dgResize();
        showSystemConfigDetails(1, initPageSize);
    }

    //初始化键盘enter事件
    $(document).keydown(function (event) {
        //兼容 IE和firefox 事件
        var e = window.event || event;
        var k = e.keyCode || e.which || e.charCode;
        //兼容 IE,firefox 兼容
        var obj = e.srcElement ? e.srcElement : e.target;
        //绑定键盘事件为 id是指定的输入框才可以触发键盘事件 13键盘事件
        if (k == "13" && (obj.id == "companyName" || obj.id == "companyContacts" || obj.id == "companyAddress"
                || obj.id == "companyTel" || obj.id == "companyFax" || obj.id == "companyPostCode")) {
            $("#saveSystemConfig").click();
        }

        //搜索按钮添加快捷键
        if (k == "13" && (obj.id == "searchCompanyName")) {
            $("#searchBtn").click();
        }
    });

    //分页信息处理
    function ininPager() {
        try {
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            pager.pagination({
                onSelectPage: function (pageNum, pageSize) {
                    opts.pageNumber = pageNum;
                    opts.pageSize = pageSize;
                    pager.pagination('refresh', {
                        pageNumber: pageNum,
                        pageSize: pageSize
                    });
                    showSystemConfigDetails(pageNum, pageSize);
                }
            });
        }
        catch (e) {
            $.messager.alert('异常处理提示', "分页信息异常 :  " + e.name + ": " + e.message, 'error');
        }
    }

    //删除系统配置信息
    function deleteSystemConfig(systemConfigId) {
        $.messager.confirm('删除确认', '确定要删除此系统配置信息吗？', function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: "/systemConfig/batchDeleteSystemConfigByIds",
                    dataType: "json",
                    data: ({
                        ids: systemConfigId
                    }),
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                        } else if(res.code == -1) {
                            $.messager.alert('提示', '演示用户禁止操作！', 'warning');
                        } else {
                            $.messager.alert('删除提示', '删除系统配置信息失败，请稍后再试！', 'error');
                        }
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('删除提示', '删除系统配置信息异常，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }

    //批量删除系统配置
    function batDeleteSystemConfig() {
        var row = $('#tableData').datagrid('getChecked');
        if (row.length == 0) {
            $.messager.alert('删除提示', '没有记录被选中！', 'warning');
            return;
        }
        if (row.length > 0) {
            $.messager.confirm('删除确认', '确定要删除选中的' + row.length + '条系统配置信息吗？', function (r) {
                if (r) {
                    var ids = "";
                    for (var i = 0; i < row.length; i++) {
                        if (i == row.length - 1) {
                            ids += row[i].id;
                            break;
                        }
                        ids += row[i].id + ",";
                    }
                    $.ajax({
                        type: "post",
                        url: "/systemConfig/batchDeleteSystemConfigByIds",
                        dataType: "json",
                        async: false,
                        data: ({
                            ids: ids
                        }),
                        success: function (res) {
                            if(res && res.code === 200) {
                                $("#searchBtn").click();
                                $(":checkbox").attr("checked", false);
                            } else if(res.code === -1) {
                                $.messager.alert('提示', '演示用户禁止操作！', 'warning');
                            } else {
                                $.messager.alert('删除提示', '删除系统配置信息失败，请稍后再试！', 'error');
                            }
                        },
                        //此处添加错误处理
                        error: function () {
                            $.messager.alert('删除提示', '删除系统配置信息异常，请稍后再试！', 'error');
                            return;
                        }
                    });
                }
            });
        }
    }

    //增加系统配置
    var url;
    var systemConfigId = 0;
    //保存编辑前的名称
    var oldCompanyName = "";
    var pageTotal = 0; //数据总量

    function addSystemConfig() {
        if(checkPower()){
            return;
        }
        if(pageTotal>=1) {
            $.messager.alert('提示', '只能填写一条系统配置信息', 'warning');
        } else {
            $('#systemConfigDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;增加系统配置');
            $(".window-mask").css({width: webW, height: webH});
            $('#systemConfigFM').form('clear');
            var row = {};
            $('#systemConfigFM').form('load', row);
            $("#companyName").focus();
            oldCompanyName = "";
            systemConfigId = 0;
            url = '/systemConfig/add';
        }
    }

    //保存系统配置信息
    $("#saveSystemConfig").off("click").on("click", function () {
        if (checkCompanyName()) {
            return;
        }
        var companyName = $.trim($("#companyName").textbox("getValue"));
        if (!companyName) {
            $.messager.alert('提示', '公司名称不能为空', 'warning');
            $("#companyName").val("").focus();
            return;
        }
        var infoObj = $("#systemConfigFM").serializeObject();
        infoObj.depotFlag = $("#depotFlag").is(':checked')?"1":"0";
        infoObj.customerFlag = $("#customerFlag").is(':checked')?"1":"0";
        infoObj.minusStockFlag = $("#minusStockFlag").is(':checked')?"1":"0";
        $.ajax({
            url: url,
            type: "post",
            dataType: "json",
            data: {
                info: JSON.stringify(infoObj)
            },
            success: function(res) {
                if(res) {
                    if(res.code === 200) {
                        $('#systemConfigDlg').dialog('close');
                        //加载完以后重新初始化
                        var opts = $("#tableData").datagrid('options');
                        showSystemConfigDetails(opts.pageNumber, opts.pageSize);
                    } else if(res.code === -1) {
                        $.messager.alert('提示', '演示用户禁止操作！', 'warning');
                    }
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('提示', '网络异常，请稍后再试！', 'error');
                return;
            }
        });
    });

    //编辑系统配置信息
    function editSystemConfig(index) {
        var res = $("#tableData").datagrid("getRows")[index];
        var row = {
            companyName: res.companyName,
            companyContacts: res.companyContacts,
            companyAddress: res.companyAddress,
            companyTel: res.companyTel,
            companyFax: res.companyFax,
            companyPostCode: res.companyPostCode
        };
        oldCompanyName = res.companyName;
        $('#systemConfigDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/pencil.png"/>&nbsp;编辑系统配置信息');
        $(".window-mask").css({width: webW, height: webH});
        $('#systemConfigFM').form('load', row);
        systemConfigId = res.id;
        $("#depotFlag").attr("checked", res.depotFlag == '1' ? true : false);
        $("#customerFlag").attr("checked", res.customerFlag == '1' ? true : false);
        $("#minusStockFlag").attr("checked", res.minusStockFlag == '1' ? true : false);
        url = '/systemConfig/update?id=' + res.id;
    }

    //检查系统配置 名称是否存在 ++ 重名无法提示问题需要跟进
    function checkCompanyName() {
        var companyName = $.trim($("#companyName").textbox("getValue"));
        //表示是否存在 true == 存在 false = 不存在
        var flag = false;
        //开始ajax名称检验，不能重名
        if (companyName.length > 0 && (oldCompanyName.length == 0 || companyName != oldCompanyName)) {
            $.ajax({
                type: "get",
                url: "/systemConfig/checkIsNameExist",
                dataType: "json",
                async: false,
                data: ({
                    id: systemConfigId,
                    name: companyName
                }),
                success: function (res) {
                    if(res && res.code === 200) {
                        if(res.data && res.data.status) {
                            flag = res.data.status;
                            if (flag) {
                                $.messager.alert('提示', '系统配置名称已经存在', 'info');
                                return;
                            }
                        }
                    }
                },
                //此处添加错误处理
                error: function () {
                    $.messager.alert('提示', '检查系统配置名称是否存在异常，请稍后再试！', 'error');
                    return;
                }
            });
        }
        return flag;
    }

    //搜索处理
    $("#searchBtn").unbind().bind({
        click: function () {
            showSystemConfigDetails(1, initPageSize);
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            opts.pageNumber = 1;
            opts.pageSize = initPageSize;
            pager.pagination('refresh', {
                pageNumber: 1,
                pageSize: initPageSize
            });
        }
    });

    function showSystemConfigDetails(pageNo, pageSize) {
        var companyName = $.trim($("#searchCompanyName").textbox("getValue"));
        $.ajax({
            type: "get",
            url: "/systemConfig/list",
            dataType: "json",
            data: ({
                search: JSON.stringify({
                    companyName: companyName
                }),
                currentPage: pageNo,
                pageSize: pageSize
            }),
            success: function (res) {
                if(res && res.code === 200){
                    if(res.data && res.data.page) {
                        pageTotal = res.data.page.total;
                        $("#tableData").datagrid('loadData', res.data.page);
                    }
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('查询提示', '查询数据后台异常，请稍后再试！', 'error');
                return;
            }
        });
    }

    //重置按钮
    $("#searchResetBtn").unbind().bind({
        click: function () {
            $("#searchCompanyName").textbox("clear");
            //加载完以后重新初始化
            $("#searchBtn").click();
        }
    });
</script>
</body>
</html>
